/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia;




import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author ronildo
 */
public class Fusioncharts {

    private static final Logger logger = LoggerFactory.getLogger();

    private static String getPath(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        return request.getPathTranslated().replaceAll("logon.xhtml", "") + "/xml/fusioncharts/login_folder/";
    }

    public static void makeXmlSmsSend(Map<String, Integer> listSmsSend, List<String> keys, Long pkLogin){
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.newDocument();

            Element chart = document.createElement("chart");
            chart.setAttribute("palette" , "2");
            chart.setAttribute("showValues" , "0");

            List<Element> elements = new LinkedList<Element>();            
            for(int i = 0; i < listSmsSend.size(); i++){
                int qtd = listSmsSend.get(keys.get(i));
                
                Element set = document.createElement("set");
                set.setAttribute("label", keys.get(i));
                set.setAttribute("value", String.valueOf(qtd));
                elements.add(set);
            }

            Element styles = document.createElement("styles");

            Element definition = document.createElement("definition");

            Element style1 = document.createElement("style");
            style1.setAttribute("name", "Anim1");
            style1.setAttribute("type","animation");
            style1.setAttribute("param","_xscale");
            style1.setAttribute("start","0");
            style1.setAttribute("duration","1");

            Element style2 = document.createElement("style");
            style2.setAttribute("name", "Anim2");
            style2.setAttribute("type","animation");
            style2.setAttribute("param","_alpha");
            style2.setAttribute("start","0");
            style2.setAttribute("duration","1");

            Element style3 = document.createElement("style");
            style3.setAttribute("name", "DataShadow");
            style3.setAttribute("type","Shadow");
            style3.setAttribute("alpha","20");

            Element application = document.createElement("application");

            Element apply1 = document.createElement("apply");
            apply1.setAttribute("toObject", "DIVLINES");
            apply1.setAttribute("styles","Anim1");

            Element apply2 = document.createElement("apply");
            apply2.setAttribute("toObject", "HGRID");
            apply2.setAttribute("styles","Anim2");

            Element apply3 = document.createElement("apply");
            apply3.setAttribute("toObject", "DATALABELS");
            apply3.setAttribute("styles","DataShadow,Anim2");

            definition.appendChild(style1);
            definition.appendChild(style2);
            definition.appendChild(style3);

            application.appendChild(apply1);
            application.appendChild(apply2);
            application.appendChild(apply3);

            styles.appendChild(definition);
            styles.appendChild(application);

            for(Element element : elements)
                chart.appendChild(element);
            chart.appendChild(styles);
            document.appendChild(chart);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer xformer = tf.newTransformer();

            xformer.setOutputProperty(OutputKeys.METHOD, "xml");
            xformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");

            File file = new File(getPath()+pkLogin+"sms_send_month.xml");
            logger.info("path of sms_send_month.xml: " + file.getAbsolutePath());
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(file);
            xformer.transform(source,result);
        }catch(TransformerConfigurationException e){
            e.printStackTrace();
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(TransformerException e){
            e.printStackTrace();
        }
    }

    public static void makeXmlSmsReceived(Map<String, Integer> listSmsReceived, List<String> keys, Long pkLogin){
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.newDocument();

            Element chart = document.createElement("chart");
            chart.setAttribute("palette" , "2");
            chart.setAttribute("showValues" , "0");

            List<Element> elements = new LinkedList<Element>();
            for(int i = 0; i < listSmsReceived.size(); i++){
                int qtd = listSmsReceived.get(keys.get(i));
                
                Element set = document.createElement("set");
                set.setAttribute("label", keys.get(i));
                set.setAttribute("value", String.valueOf(qtd));
                elements.add(set);
            }

            Element styles = document.createElement("styles");

            Element definition = document.createElement("definition");

            Element style1 = document.createElement("style");
            style1.setAttribute("name", "Anim1");
            style1.setAttribute("type","animation");
            style1.setAttribute("param","_xscale");
            style1.setAttribute("start","0");
            style1.setAttribute("duration","1");

            Element style2 = document.createElement("style");
            style2.setAttribute("name", "Anim2");
            style2.setAttribute("type","animation");
            style2.setAttribute("param","_alpha");
            style2.setAttribute("start","0");
            style2.setAttribute("duration","1");

            Element style3 = document.createElement("style");
            style3.setAttribute("name", "DataShadow");
            style3.setAttribute("type","Shadow");
            style3.setAttribute("alpha","20");

            Element application = document.createElement("application");

            Element apply1 = document.createElement("apply");
            apply1.setAttribute("toObject", "DIVLINES");
            apply1.setAttribute("styles","Anim1");

            Element apply2 = document.createElement("apply");
            apply2.setAttribute("toObject", "HGRID");
            apply2.setAttribute("styles","Anim2");

            Element apply3 = document.createElement("apply");
            apply3.setAttribute("toObject", "DATALABELS");
            apply3.setAttribute("styles","DataShadow,Anim2");

            definition.appendChild(style1);
            definition.appendChild(style2);
            definition.appendChild(style3);

            application.appendChild(apply1);
            application.appendChild(apply2);
            application.appendChild(apply3);

            styles.appendChild(definition);
            styles.appendChild(application);

            for(Element element : elements)
                chart.appendChild(element);
            chart.appendChild(styles);
            document.appendChild(chart);

            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer xformer = tf.newTransformer();

            xformer.setOutputProperty(OutputKeys.METHOD, "xml");
            xformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");

            File file = new File(getPath()+pkLogin+"sms_received_month.xml");
            logger.info("path of sms_received_month.xml: " + file.getAbsolutePath());
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(file);
            xformer.transform(source,result);
        }catch(TransformerConfigurationException e){
            e.printStackTrace();
        }catch(ParserConfigurationException e){
            e.printStackTrace();
        }catch(TransformerException e){
            e.printStackTrace();
        }
    }
}
